package com.bdqn.ch03thymeleaf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bdqn.ch03thymeleaf.entity.Account;
import com.bdqn.ch03thymeleaf.entity.TransactionRecord;
import com.bdqn.ch03thymeleaf.mapper.TransactionRecordMapper;
import com.bdqn.ch03thymeleaf.mapper.TransferMapper;
import com.bdqn.ch03thymeleaf.service.TransferService;
import com.bdqn.ch03thymeleaf.utils.Md5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;

@Service
public class TransferServiceImpl implements TransferService {
    @Autowired
    TransferMapper transferMapper;
    @Autowired
    TransactionRecordMapper transactionRecordMapper;

    @Override
    public Account getLogin(String cardno, String password) {
        QueryWrapper<Account> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("cardno", cardno)
                .eq("password", Md5Util.encrypt(password));
        return transferMapper.selectOne(queryWrapper);
    }

    @Override
    public Account getAccount(String cardno) {
        QueryWrapper<Account> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("cardno", cardno);
        return transferMapper.selectOne(queryWrapper);
    }

    @Override
    public int updateTransfer(Account account) {
        QueryWrapper<Account> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("cardno", account.getCardno());
        Account account1 = transferMapper.selectOne(queryWrapper);
        Long id = account1.getId();
        account.setId(id);
        if (account.getPassword() != null) {
            account.setPassword(account.getPassword());
        }
        if (account.getBalance() != null) {
            account.setBalance(account.getBalance());
        }
        return transferMapper.updateById(account);
    }


    @Transactional
    @Override
    public void transfer(String fromCard, String toCard, BigDecimal amount) throws Exception {
//        // 1. 验证转出账户
//        QueryWrapper<Account> queryWrapper1 = new QueryWrapper<>();
//        queryWrapper1.eq("cardno", fromCard);
//        Account fromAccount = transferMapper.selectOne(queryWrapper1);
//        BigDecimal balance = BigDecimal.valueOf(fromAccount.getBalance());
//        if (fromAccount == null) {
//            throw new Exception("转出账户不存在");
//        }
//        if (fromAccount.getBalance().compareTo(amount) < 0) {
//            throw new Exception("金额不足");
//        }
//        // 4. 验证转入账户
//        QueryWrapper<Account> queryWrapper2 = new QueryWrapper<>();
//        queryWrapper2.eq("cardno", toCard);
//        Account toAccount = transferMapper.selectOne(queryWrapper2);
//        ;
//        BigDecimal balance2 = BigDecimal.valueOf(toAccount.getBalance());
//        if (toAccount == null) {
//            throw new Exception("转入账户不存在");
//        }
//        BigDecimal newBalance = balance.subtract(amount);
//        BigDecimal newBalance2 = balance2.add(amount);
//        // 5. 执行转账
//        fromAccount.setBalance(newBalance);
//        toAccount.setBalance(newBalance2);
//        // 6. 更新数据库
//        Long id1 = fromAccount.getId();
//        fromAccount.setId(id1);
//        transferMapper.updateById(fromAccount);
////        System.out.println(1/0);
//        Long id2 = toAccount.getId();
//        toAccount.setId(id2);
//        transferMapper.updateById(toAccount);
    }

    @Override
    public int addAccount(TransactionRecord transactionRecord) {
        return transactionRecordMapper.insert(transactionRecord);
    }

    @Override
    public Page getPage(String cardno, Date startDate, Date endDate, Integer current, Integer size) {
        QueryWrapper<TransactionRecord> wrapper = new QueryWrapper<TransactionRecord>();
        wrapper.eq("cardno", cardno);
        // 时间范围条件（可选）
        if (startDate != null) {
            wrapper.ge("transactionDate", startDate); // 大于等于开始时间
        }
        if (endDate != null) {
            wrapper.le("transactionDate", endDate); // 小于等于结束时间
        }
        //当前页和每页显示数量
        Page<TransactionRecord> page = new Page<>(current, size);
        IPage<TransactionRecord> transactionRecordIPage = transactionRecordMapper.selectPage(page, wrapper);
        return (Page) transactionRecordIPage;
    }
}
